package com.hdoit.ares.logical.acc.dao.impl;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import com.hdoit.ares.logical.acc.dao.IAccountRechargeDao;
import com.hdoit.ares.logical.acc.model.AccountRecharge;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.viewmodel.acc.AccountRechargeSearch;
import com.tennetcn.common.dao.base.IFinderExpression;
import com.tennetcn.common.dao.base.impl.SuperDao;
import com.tennetcn.common.enums.OrderEnum;
import com.tennetcn.common.enums.pub.YesOrNo;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.FinderExpressionFactory;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年11月24日 10:14:37
 * @comment
 */
@Component
public class AccountRechargeDaoImpl extends SuperDao<AccountRecharge> implements IAccountRechargeDao{

	@Override
	public List<AccountRecharge> queryList(AccountRechargeSearch search, PagerModel pagerModel) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT accountRecharge.*","customer.username as customerName","customer.userIdentity as userIdentity")
						.from(AccountRecharge.class,"accountRecharge")
						.leftJoin(Customer.class,"customer")
						.on("accountRecharge.customerId", "customer.id")
						.andWhere("accountRecharge.deleteMark=#{deleteMark}")
						.setParam("deleteMark", YesOrNo.no)
						.setMainTableAlias("accountRecharge")
						.addOrder("accountRecharge.rechargeDate", OrderEnum.desc);
		
		appendFinder(search,finderExpression);

        if(pagerModel == null){
            return queryList(finderExpression, search); 
          }
        return queryList(finderExpression, search, pagerModel);
	}

	private void appendFinder(AccountRechargeSearch search, IFinderExpression finder) {
		if(!StringUtils.isEmpty(search.getRechagerNum())){
			finder.andWhere("accountRecharge.rechagerNum like #{rechagerNum}")
				  .setParam("rechagerNum", "%"+search.getRechagerNum()+"%");
		}
		
		if(search.getStartMoney()!= null){
			finder.andWhere("accountRecharge.rechargeMoney >= #{startMoney}")
				  .setParam("startMoney", search.getStartMoney());
		}
		if(search.getEndMoney()!= null){
			finder.andWhere("accountRecharge.rechargeMoney <= #{startMoney}")
				  .setParam("endMoney", search.getEndMoney());
		}
		
		if(search.getStartDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{startDate},accountRecharge.rechargeDate)>=0")
				  .setParam("startDate", search.getStartDate());
		}
		if(search.getEndDate()!=null){
			finder.andWhere("TIMESTAMPDIFF(SECOND,#{endDate},accountRecharge.rechargeDate)<=0")
				  .setParam("endDate", search.getEndDate());
		}
		
		if(!StringUtils.isEmpty(search.getAccount())){
			finder.andWhere("accountRecharge.account=#{account}")
			  	  .setParam("account", search.getAccount());
		}
		if(!StringUtils.isEmpty(search.getAccountNo())){
			finder.andWhere("accountRecharge.accountNo=#{accountNo}")
			  	  .setParam("accountNo", search.getAccountNo());
		}		
		if(!StringUtils.isEmpty(search.getCustomerName())){
			finder.andWhere("customer.username like #{username}")
				  .setParam("username", "%"+search.getCustomerName()+"%");
		}
		if(!StringUtils.isEmpty(search.getUserIdentity())){
			finder.andWhere("customer.userIdentity like #{userIdentity}")
				  .setParam("userIdentity", "%"+search.getUserIdentity()+"%");
		}		
	}

/*    @Override
    public AccountRecharge queryModel(String id){
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("DISTINCT accountRecharge.*,customer.username as customerName")
		.from(AccountRecharge.class,"accountRecharge")
		.leftJoin(Customer.class,"customer")
		.on("accountRecharge.customerId", "customer.id")
		.andEq("accountRecharge.id", id);
	    return queryModel(finderExpression);
    }*/

	@Override
	public int queryCount(AccountRechargeSearch search) {
		IFinderExpression finderExpression=FinderExpressionFactory.createExpression();
		finderExpression.select("count(DISTINCT accountRecharge.id) as count")
						.from(AccountRecharge.class,"accountRecharge")
						.leftJoin(Customer.class,"customer")
						.on("accountRecharge.customerId", "customer.id")
						.andWhere("accountRecharge.deleteMark=#{deleteMark}")
		                .setParam("deleteMark", YesOrNo.no)					
						.setMainTableAlias("accountRecharge");
		
		appendFinder(search,finderExpression);
		return queryCount(finderExpression, search);
	}

	@Override
	public List<AccountRecharge> queryList(AccountRechargeSearch search) {
		return queryList(search,null);
	}

}
